package cs2340.todolist.controller;

import java.util.Date;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TimePicker;

import com.google.android.maps.GeoPoint;

import cs2340.todolist.R;
import cs2340.todolist.application.ToDoListApplication;
import cs2340.todolist.model.account_management.AccountManager;
import cs2340.todolist.model.account_management.ItemInformationManager;
import cs2340.todolist.model.utility.SearchEngine.CombinedFilter;
import cs2340.todolist.model.utility.SearchEngine.FilterCompleted;
import cs2340.todolist.model.utility.SearchEngine.FilterDate;
import cs2340.todolist.model.utility.SearchEngine.FilterNearLocation;
import cs2340.todolist.model.utility.SearchEngine.FilterType;
import cs2340.todolist.model.utility.SearchEngine.NullFilter;


public class FilterActivity extends Activity {
	
	private AccountManager accountManager;
	
	/**
	 * called when the activity is first created
	 */
	public void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.filter_activity);
		
		//gets the fields on the display
        final Spinner typeSpinner = (Spinner)findViewById(R.id.filter_type_spinner);	
        final DatePicker dueDatePicker = (DatePicker)findViewById(R.id.filter_date_picker);
        final TimePicker timePicker = (TimePicker)findViewById(R.id.filter_time_picker);
        final CheckBox typeCheckBox = (CheckBox)findViewById(R.id.filter_type_checkbox);
        final CheckBox dateCheckBox = (CheckBox)findViewById(R.id.filter_date_checkbox);
        final CheckBox completedCheckBox = (CheckBox)findViewById(R.id.filter_completed_checkbox);
		final CheckBox filterNearMeCheckBox = (CheckBox)findViewById(R.id.filter_items_near_me);
		final EditText filterRangeEditText = (EditText)findViewById(R.id.filter_activity_filter_location_range);
		final Button filterButton = (Button) findViewById(R.id.testingbutton);
		
		
		
        filterButton.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View arg0) {
				//if the type checkbox is checked, set the filter appropriately
				CombinedFilter combinedFilter = new CombinedFilter();
				combinedFilter.add(new NullFilter());
				if(typeCheckBox.isChecked()) {
					combinedFilter.add(new FilterType(typeSpinner.getSelectedItemPosition()));
				}
				
				//if the completed checkbox is checked, set the filter on
				if(completedCheckBox.isChecked()) {
					combinedFilter.add(new FilterCompleted());
				}
				
				//if the date checkbox is checked, filter by the date
				if(dateCheckBox.isChecked()) {
					combinedFilter.add(new FilterDate(new Date(dueDatePicker.getYear(),dueDatePicker.getMonth(),dueDatePicker.getDayOfMonth(),timePicker.getCurrentHour(),timePicker.getCurrentMinute())));
				}
				
				if(filterNearMeCheckBox.isChecked()) {
					LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
					Location lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
					GeoPoint pivot = new GeoPoint((int)(lastKnownLocation.getLatitude()*1E6),(int)(lastKnownLocation.getLongitude()*1E6));
					int filterRange = Integer.parseInt(filterRangeEditText.getText().toString());
					combinedFilter.add(new FilterNearLocation(FilterActivity.this, pivot, filterRange));
				}
				
				//go back to home screen
				((ToDoListApplication)getApplication()).getSessionManager().setCurrentFilter(combinedFilter);
				Intent homeScreenIntent = new Intent(FilterActivity.this,HomeScreenActivity.class);
				startActivity(homeScreenIntent);
			}
        }); 
		
		final Button cancelButton = (Button) findViewById(R.id.filter_cancel_button);
        cancelButton.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View arg0) {
				//this brings up the login screen if the cancel button is pressed
				Intent homeScreenIntent = new Intent(FilterActivity.this,HomeScreenActivity.class);
				startActivity(homeScreenIntent);
			}
        });
	}
	
	/**
	 * called whenever this activity is started
	 */
	public void onStart() {

		super.onStart();

		//grabs teh account manager from background application
        accountManager = ((ToDoListApplication)getApplication()).getSessionManager().getAccountManager();
        accountManager.changeContext(this);
	}
	


}
